<template>
  <v-layout type="main">

    <div class="layout-top">
      <div class="rollback" @click="$router.push('/deploy')">
        <img src="~@/assets/img/prev.png" alt="">
        <span>返回</span>
      </div>

      <div class="code-description">
        <div class="dea_top">
          <div>
            <el-image class="nav_box" :src="detailData.model.avatar ? detailData.model.avatar : require('@/assets/img/drone3.jpg')" fit="fit"></el-image>
          </div>

          <div class="header_box">
            <div class="code-desc-header">
              <div class="title">
                <div class="title-header">
                  <span>{{ detailData.name }}</span>
                </div>

                <div class="code-tags">
                  <div class="user-tag tag">
                    <img src="~@/assets/img/user.png" alt="">
                    {{
                      detailData.create_user && (detailData.create_user.nick_name ||
                        detailData.create_user.username)
                    }}
                  </div>

                  <div :class="['status-tag tag', {
                        'danger': detailData.status === 'terminated',
                        'warning': detailData.status != 'terminated' && detailData.status != 'running',
                        'success': detailData.status === 'running'
                      }]">
                    {{ statusMap[detailData.status] }}
                  </div>

                  <div class="time-tag tag">
                    <span>创建时间：</span>
                    {{ detailData.create_time }}
                  </div>

                  <div class="time-tag tag">
                    <span>更新时间：</span>
                    {{ detailData.update_time }}
                  </div>
                </div>

                <div class="staet_box" v-if="detailData.status">
                  <plug-state :statusStr="detailData.status" />
                </div>
              </div>
            </div>

          </div>
        </div>
      </div>

      <div class="btns-container" v-if="detailData !== null">
        <div class="alick-handle-btns">
          <el-button class="handle-btn" type="success" size="medium" @click="start"
            v-show="detailData.status == 'terminated'">
            启动
          </el-button>
          <el-button class="handle-btn" type="danger" size="medium" @click="stop"
            v-show="detailData.status != 'terminated'">
            停止
          </el-button>
          <el-button class="handle-btn" type="primary" size="medium" @click="entry"
            v-show="detailData.status == 'running'">
            打开测试页
          </el-button>
        </div>
      </div>
    </div>

    <div class="tabs-left" v-if="thisRouter.length > 1">
      <el-tabs v-model="nowRouter" @tab-click="skip" v-show="!$route.meta.hideDetails">
        <el-tab-pane :label="e.meta.title" :name="e.name" v-for="(e, i) in thisRouter" :key="i" />
      </el-tabs>
    </div>

    <div :class="['layout-content', { 'layout-content-nodetail': $route.meta.hideDetails }]">
      <router-view class="content-detail" :detailData="detailData" v-if="detailData" @getDetail="getDetail" />
    </div>

    <el-dialog :visible.sync="showUpdateBaseDialog" title="模型部署基本信息" width="840px">
      <el-form class="form myself-form" ref="form" :rules="submitRules" :model="addForm" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input type="text" v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input type="textarea" :rows="3" v-model="addForm.desc" />
        </el-form-item>
        <el-form-item label=" ">
          <el-button class="none-type" type="primary" @click="commitBase">确定</el-button>
          <el-button class="none-type" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </v-layout>
</template>

<script>
import plugState from '@/components/module/plug-state'
import { getDeployDetail, putDeploy, startServing, stopServing } from '@/api/deploy'

export default {
  data() {
    return {
      nowRouter: this.$route.name,
      thisRouter: [],
      detailData: null,
      addForm: {},
      showUpdateBaseDialog: false,
      submitRules: {
        name: [
          { required: true, message: "该项为必填项" },
          { required: true, min: 1, max: 40, message: "长度在1到40个字符" },
        ],
        desc: [
          { required: true, message: "该项为必填项" },
          { required: true, min: 1, max: 200, message: "长度在1到200个字符" },
        ],
      },
    };
  },
  methods: {
    skip() {
      let path = `/deploy/${this.thisRouter.filter(e => e.name == this.nowRouter)[0].path}`
      if (path == this.$route.path) return
      this.$router.push({
        path,
        query: {
          id: this.$route.query.id
        }
      })
    },
    async getDetail() {
      if (!this.$route.query.id) return
      let res = await getDeployDetail({ id: this.$route.query.id });
      console.log('res', res)
      if (res && res.data) {
        this.detailData = res.data;
        this.addForm = JSON.parse(JSON.stringify(this.detailData))
      }
    },
    entry() {
      let path = "http://" + this.detailData.tcp_port_list[0].endpoint
      window.open(path, '_blank');
    },
    updateBase() {
      this.showUpdateBaseDialog = true
    },
    avatarPath(data) {
      this.addForm.avatar = data;
    },
    commitBase() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          let res = await putDeploy(this.addForm);
          if (res && res.data) {
            this.getDetail()
            this.showUpdateBaseDialog = false
          }
        }
      })
    },
    cancel() {
      this.showUpdateBaseDialog = false
    },
    async start() {
      await startServing(this.detailData.id);
      this.getDetail()
      this.$forceUpdate()
    },
    async stop() {
      await stopServing(this.detailData.id);
      this.getDetail()
      this.$forceUpdate()
    },

  },
  components: { plugState },
  mounted() {
    let temp = this.$router.options.routes.filter(
      e => e.name == 'deploy' && e.children
    )
    this.thisRouter = temp[0].children.filter(e => !e.meta.hideDetails)
    this.getDetail()
    this.$EventBus.$on('serving', e => {
      console.log(e)
      this.getDetail()
    })
  },
  destroyed() {
    this.$EventBus.$off('serving')
  },
}
</script>
<style lang="less" scoped>
@import "~@/assets/css/detail-layout.less";
</style>
